const buttons = document.querySelectorAll("#buttons button");
const inputs = document.querySelectorAll("#controlList input");
const table = document.querySelector("#table");
const select = document.querySelector("#condition")
const storage = window.localStorage;

//storage.setItem("list", JSON.stringify([]));
buttons[0].onclick = create;
buttons[1].onclick = remove;
buttons[2].onclick = edit;
buttons[3].onclick = initiate

function checkinput(str, flag) {
	var reg1 = /^[0-9]{0,8}$/g
	var reg3 = /^[0-9]+$/
	if (flag == 1) {
		return reg1.test(str)
	} else {
		return reg3.test(str)
	}

}

function initiate() {
	storage.setItem("list", JSON.stringify([]));
	storage.setItem("flag", false) //用于控制同步页面
	refresh()
}

function remove() {
	var tempsto = JSON.parse(storage.list)
	var id = inputs[0].value;

	if (!id.length) {
		alert("没输入")
		return 0;
	}
	if (!checkinput(inputs[0].value, 1)) {
		alert("输入有误")
		return 0;
	}
	id = "#OJE" + id
	//filter
	tempsto = tempsto.filter((str) => {
		if (id == str.split(",")[0]) {
			return 0
		} else {
			return 1
		}
	})
	storage.list = JSON.stringify(tempsto);
    storage.flag=false;
	refresh();
	return 1;
}

function edit() {
	var tempsto = JSON.parse(storage.list);
	var tempstr = "";
	var id = inputs[0].value;
	if (!id.length) {
		alert("没输入")
		return 0;
	}
	if (!checkinput(inputs[0].value, 1) || !checkinput(inputs[2].value, 3)) {
		alert("输入有误")
		return 0;
	}
	id = "#OJE" + id
	var n = 0;
	for (n = 0; n < tempsto.length; n++) {
		var str = tempsto[n]; //用于查找数组内字符串
		console.log(str);
		if (str.indexOf(`${id}`) != -1) {
			tempstr = str
			break;
		}

	}


	if (tempstr.length == 0) {
		console.log("没找到");
		return 0;
	}
	var date = new Date();
	tempstr = tempstr.split(",")[0] + "," + inputs[1].value + "," + `${date.getHours()}:${date.getMinutes()}`
	tempstr += "," + inputs[2].value + "," + select.value;
	tempsto[n] = tempstr;
	storage.list = JSON.stringify(tempsto);
storage.flag=false;
	refresh();
	//创建时间更新
	//search匹配
}

function create() {
	var strlist = ""; //创建新的内容
	var date = new Date();
	var tempsto = JSON.parse(storage.list)
	if (!checkinput(inputs[0].value, 1) || !checkinput(inputs[2].value, 3)) {
		alert("输入有误")
		return 0;
	}

	strlist = "#OJE" + inputs[0].value + ",";
	strlist += inputs[1].value + "," + `${date.getHours()}:${date.getMinutes()}`
	strlist += "," + inputs[2].value + "," + select.value;
	tempsto.push(strlist)
	storage.list = JSON.stringify(tempsto);
	storage.flag=false;
	refresh();
}

function refresh() { //更新表格,刷新自动加载+修改自动加载
	var tempsto = JSON.parse(storage.list)
	if (tempsto != 0) {
		table.innerHTML = "<tr id=table_title><th>订单编号</th><th>客户信息</th><th>创建时间</th><th>金额</th><th>状态</th></tr>"
		for (var li of tempsto) {
			var tr = document.createElement("tr");
			var str = li.split(",");
			for (var data of str) {
				var td = document.createElement("td");
				td.innerText = data
				tr.appendChild(td);
			}
			table.appendChild(tr);
		}

	} else {
		console.log("数据为空")
		return 0;
	}
}
window.onload = function() {
	
	refresh();
	return 0;
}